package com.guigu.tms.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.tms.base.mapper.BaseTruckTypeMapper;
import com.guigu.tms.base.mapper.PdGoodsTypeMapper;
import com.guigu.tms.base.mapper.PdTruckTypeGoodsTypeMapper;
import com.guigu.tms.base.mapper.PdTruckTypeMapper;
import com.guigu.tms.commons.entity.base.BaseTruckType;
import com.guigu.tms.base.service.IPdTruckTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.commons.entity.base.PdGoodsType;
import com.guigu.tms.commons.entity.base.PdTruckType;
import com.guigu.tms.commons.entity.base.PdTruckTypeGoodsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 车辆类型表 服务实现类
 * </p>
 *
 * @author wj
 * @since 2022-09-26
 */
@Service
public class PdTruckTypeServiceImpl extends ServiceImpl<PdTruckTypeMapper, PdTruckType> implements IPdTruckTypeService {

    @Autowired
    PdTruckTypeMapper truckTypeMapper;

    @Autowired
    BaseTruckTypeMapper baseTruckTypeMapper;

    @Autowired
    PdGoodsTypeMapper pdGoodsTypeMapper;

    @Autowired
    PdTruckTypeGoodsTypeMapper pdTruckTypeGoodsTypeMapper;


    @Override
    public List<PdTruckType> ShowPdTruckType() {
        return truckTypeMapper.ShowPdTruckType();
    }

    @Override
    public PageInfo<BaseTruckType> BaseTruckTypeList(Map<String,String> map) {
        String pageindex=map.get("pageindex");
        String pagenum=map.get("pagenum");
        int page=Integer.valueOf(pageindex)<=0?1:Integer.valueOf(pageindex);
        PageHelper.startPage(page,Integer.valueOf(pagenum));
        List<BaseTruckType> pdTrucks = baseTruckTypeMapper.BaseTruckTypeList(map);
        return new PageInfo<>(pdTrucks);
    }

    @Override
    public List<PdGoodsType> ShowPdGoodsType() {
        return pdGoodsTypeMapper.selectList(null);
    }

    @Override
    public int AddTruckType(PdTruckType pdTruckType) {
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (999 - 100 + 1)) + 100;
        String s=str+rannum;
        pdTruckType.setId(s);
        int insert = truckTypeMapper.insert(pdTruckType);
        List<Integer> ids = pdTruckType.getIds();
        Integer truckTypeId = pdTruckType.getTruckTypeId();
        for(int i = 0; i < ids.size(); i++){
            PdTruckTypeGoodsType pdTruckTypeGoodsType=new PdTruckTypeGoodsType();
            pdTruckTypeGoodsType.setTruckTypeId(String.valueOf(truckTypeId));
            pdTruckTypeGoodsType.setGoodsTypeId(String.valueOf(ids.get(i)));
            pdTruckTypeGoodsTypeMapper.insert(pdTruckTypeGoodsType);
        }

        return insert;
    }

    @Override
    public int DeleteTruckTypeById(int truckTypeId) {
        return truckTypeMapper.deleteById(truckTypeId);
    }

    @Override
    public int UpdateTruckType(PdTruckType pdTruckType) {
        int updateById = truckTypeMapper.updateById(pdTruckType);
        List<Integer> ids = pdTruckType.getIds();
        Integer truckTypeId = pdTruckType.getTruckTypeId();
        for(int i = 0; i < ids.size(); i++){
            System.out.println(ids.get(i));
            PdTruckTypeGoodsType pdTruckTypeGoodsType=new PdTruckTypeGoodsType();
            pdTruckTypeGoodsType.setTruckTypeId(String.valueOf(truckTypeId));
            pdTruckTypeGoodsType.setGoodsTypeId(String.valueOf(ids.get(i)));
            pdTruckTypeGoodsTypeMapper.insert(pdTruckTypeGoodsType);
        }
        return updateById;
    }

    @Override
    public int DeleteGoodsTruck(int truckTypeId) {
        return pdTruckTypeGoodsTypeMapper.DeleteGoodsTruck(truckTypeId);
    }
}
